package com.lzh.media.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzh.common.pojo.PageInfo;
import com.lzh.common.pojo.PageRequestDto;
import com.lzh.common.pojo.Result;
import com.lzh.common.utils.RequestContextUtil;
import com.lzh.media.dto.NewsDto;
import com.lzh.media.dto.NewsQueryDto;
import com.lzh.media.entity.WmNews;
import com.lzh.media.service.WmNewsService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.lzh.core.controller.AbstractCoreController;

/**
* <p>
* 自媒体图文内容信息表 控制器</p>
* @author lzh
* @since 2021-06-24
*/
@RestController
@RequestMapping("/wmNews")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    //注入
    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService = wmNewsService;
    }

    @GetMapping("/one")
    public Result one(@RequestParam(value = "id") Integer id) {
       NewsDto newsDto = wmNewsService.one(id);
        return  Result.ok(newsDto);
    }

    /**
     * 添加或修改文章
     * @param newsDto 添加或修改文章对象
     * @return
     */
    @PostMapping("/modify")
    public Result modify(@RequestBody NewsDto newsDto) {
        wmNewsService.modify(newsDto);
        return Result.ok();
    }

    /**
     * 分页查询
     * @param newsQueryDto
     * @return
     */
    @PostMapping("/searchPage")
    public Result search(@RequestBody NewsQueryDto newsQueryDto) {
        //解析令牌
        Object user_id = RequestContextUtil.getToken("token", "id");
        System.out.println(user_id);
        //封装分页信息
        Page<WmNews> wmNewsPage = new Page<>(newsQueryDto.getPage(),newsQueryDto.getSize());
        //封装查询条件
        QueryWrapper<WmNews> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(user_id)) {
            queryWrapper.eq("user_id",Integer.valueOf(user_id.toString()));
        }
        //判断查询条件是否为空
        if (newsQueryDto != null) {
            //设置查询状态
            if (!StringUtils.isEmpty(newsQueryDto.getStatus())) {
                queryWrapper.eq("status", newsQueryDto.getStatus());
            }
            //设置查询标题
            if (!StringUtils.isEmpty(newsQueryDto.getKeyword())) {
                queryWrapper.eq("title", newsQueryDto.getKeyword());
            }
            //设置查询频道
            if (!StringUtils.isEmpty(newsQueryDto.getChannelId())) {
                queryWrapper.eq("channel_id", newsQueryDto.getChannelId());
            }
            //根据发布时间查询
            if (!StringUtils.isEmpty(newsQueryDto.getStartTime()) && !StringUtils.isEmpty(newsQueryDto.getEndTime())) {
                queryWrapper.notBetween("submited_time", newsQueryDto.getStartTime(), newsQueryDto.getEndTime());
            }
        }
        //执行分页查询
        IPage<WmNews> page = wmNewsService.page(wmNewsPage,queryWrapper);
        //封装分页结果集 当前页，当前页数据大小，数据总条数，页码总数，当前页数据
        PageInfo<WmNews> pageInfo = new PageInfo<>(page.getCurrent(),page.getSize(),page.getTotal(),page.getPages(),page.getRecords());
        return Result.ok(pageInfo);
    }
}

